package com.example.zhanghf.hello;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
    private EditText et = null;
    public final static int REQUEST_CODE_ONE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        TextView tv = new TextView(this);
//        tv.setText("XPP");
//        setContentView(tv);
        TextView tv = new TextView(this);
        tv = (TextView)findViewById(R.id.tv);
        String str = "有email:zhanghaifeng1414@126.com 有电话：12345555555";
        SpannableStringBuilder style = new SpannableStringBuilder(str);
        style.setSpan(new ForegroundColorSpan(Color.RED), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        tv.setText(style);



        et = (EditText)findViewById(R.id.edit3);
        et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                Toast.makeText(MainActivity.this, String.valueOf(actionId), Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        //获取EditText文本
        Button getValue=(Button)findViewById(R.id.btn_get_value);
        getValue.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, et.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        });
        //让EditText全选
        Button all=(Button)findViewById(R.id.btn_all);
        all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et.selectAll();
            }
        });
        //从第2个字符开始选择EditText文本
        Button select=(Button)findViewById(R.id.btn_select);
        select.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Editable editable=et.getText();
                Selection.setSelection(editable, 1, editable.length());
            }
        });
        //获取选中的文本
        Button getSelect=(Button)findViewById(R.id.btn_get_select);
        getSelect.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                int start=et.getSelectionStart();
                int end=et.getSelectionEnd();
                CharSequence selectText=et.getText().subSequence(start, end);
                Toast.makeText(MainActivity.this, selectText, Toast.LENGTH_SHORT).show();
            }
        });
        // 清空输入框
        final Button clearbtn = (Button)findViewById(R.id.btn_clear);
        clearbtn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, et.getText().toString(), Toast.LENGTH_SHORT).show();
                et.setText("");
            }
        });

        Button btn = (Button)findViewById(R.id.second);
        btn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, SecondActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("str", "这是传递的数据");
                intent.putExtras(bundle);
                //startActivity(intent);
                startActivityForResult(intent, REQUEST_CODE_ONE);
                //startActivity(new Intent(MainActivity.this, SecondActivity.class));
                //finish();
            }
        });

        Button testbtn1 = (Button)findViewById(R.id.btn_test1);
        Button testbtn2 = (Button)findViewById(R.id.btn_test2);

        testbtn1.setOnClickListener(listener);
        testbtn2.setOnClickListener(listener);

        Button layoutbtn = (Button)findViewById(R.id.layout_demo);

        layoutbtn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, LayoutDemo.class);
                startActivity(intent);
            }
        });
        Button relative_btn = (Button)findViewById(R.id.relative_layout_demo);

        relative_btn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, RelativeLayoutDemo.class);
                startActivity(intent);
            }
        });

        Button table_btn = (Button)findViewById(R.id.table_layout_demo);

        table_btn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, TableLayoutDemo.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Toast.makeText(MainActivity.this, "xxx", Toast.LENGTH_SHORT).show();
        if (requestCode == REQUEST_CODE_ONE){
            if(resultCode == SecondActivity.RESULT_CODE_ONE){
                Bundle bundle = data.getExtras();
                Toast.makeText(MainActivity.this,bundle.getString("back").toString(),Toast.LENGTH_SHORT).show();
            }
        }
    }

    private Button.OnClickListener listener = new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
             Button btn = (Button)v;
            switch (btn.getId()){
                case R.id.btn_test1:
                    Toast.makeText(MainActivity.this, "你点击了第一个按钮",Toast.LENGTH_LONG).show();
                    break;
                case R.id.btn_test2:
                    Toast.makeText(MainActivity .this, "你点击了第二个按钮",Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
